﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 医院药库管理系统
{
    public partial class 厂商资料新建和维护 : Form
    {
        public 厂商资料新建和维护()
        {
            InitializeComponent();
        }

        private void bt_csfh_Click(object sender, EventArgs e)
        {
            首页 f3 = new 首页();
            f3.Show();
        }

        private void bt_tj_Click(object sender, EventArgs e)
        {
            if (tb_bh.Text == "" || tb_gsd.Text == "" || tb_mc.Text == "")
            {
                MessageBox.Show("基本信息不能为空！");
            }

            SqlConnection sqlConnection = new SqlConnection();

            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
             SqlCommand sqlCommand4 = sqlConnection.CreateCommand();
            sqlCommand4.CommandText = "select count(*) from tb_Firm ;";

            sqlConnection.Open();
            
            object obj1 = sqlCommand4.ExecuteScalar();
            sqlConnection.Close();
          
            int n = (int)obj1;
            
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
               "INSERT tb_Firm (No,Name,Attribution) VALUES(" + (n + 1) + ",@Name,@Attribution);";
            
            sqlCommand.Parameters.AddWithValue("@Name", this.tb_mc.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Attribution", this.tb_gsd.Text.Trim());
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            if (rowAffected == 1)
            {
                MessageBox.Show("新建成功。");
            }
            else
            {
                MessageBox.Show("新建失败！");

            }
            sqlCommand.CommandText = "SELECT * FROM tb_Firm;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_cszl.DataSource = csTable;
            this.dgv_cszl.Columns["No"].ReadOnly = true;                                 
        }

        private void 厂商资料新建和维护_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Firm;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_cszl.DataSource = csTable;
            this.dgv_cszl.Columns["No"].HeaderText = "编号";
            this.dgv_cszl.Columns["Name"].HeaderText = "名称";
            this.dgv_cszl.Columns["Attribution"].HeaderText = "归属地";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE tb_Firm"
                + " SET No=@NewNo,Name=@Name,Attribution=@Attribution"
                + " WHERE No=@OldNo;";
            updateCommand.Parameters.Add("@NewNo", SqlDbType.Int, 0, "No");                              
            updateCommand.Parameters.Add("@Name", SqlDbType.VarChar, 100, "Name");
            updateCommand.Parameters.Add("@Attribution", SqlDbType.VarChar, 20, "Attribution");
            updateCommand.Parameters.Add("@OldNo", SqlDbType.Int, 0, "No");                             
            updateCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
            SqlCommand deleteCommand = new SqlCommand();                                                    //声明并实例化SQL命令；该命令用于删除；
            deleteCommand.Connection = sqlConnection;                                                       //将SQL命令的连接属性指向SQL连接；
            deleteCommand.CommandText =                                                                     //指定SQL命令的命令文本；
                "DELETE tb_Firm"
                + " WHERE No=@No;";
            deleteCommand.Parameters.Add("@No", SqlDbType.Int , 0, "No");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.UpdateCommand = updateCommand;
            sqlDataAdapter.DeleteCommand = deleteCommand;
            DataTable csTable1 = (DataTable)this.dgv_cszl.DataSource;                                 
            sqlConnection.Open();                                                                           
            int rowAffected = sqlDataAdapter.Update(csTable1);                                         
            sqlConnection.Close();                                                                          
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");    
        }
    }
}
